package com.stryker;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.stryker.model.User;
import com.stryker.network.CallService;
import com.stryker.ultilities.Application;
import com.stryker.ultilities.Ultilities;

// TODO: Auto-generated Javadoc
/**
 * The Class StrykersActivity.
 */
public class StrykersActivity extends Activity implements OnClickListener {
	
	/** The Constant SOAP_ACTION. */
	private static final String SOAP_ACTION = "http://tempuri.org/M_Login";
	
	/** The Constant METHOD_NAME. */
	private static final String METHOD_NAME = "M_Login";
	
	/** The Constant NAMESPACE. */
	private static final String NAMESPACE = "http://tempuri.org/";
	
	/** The Constant URL. */
	private static final String URL = "http://scsandroidapp.strykercontractingservicesllc.com/UserLogin.asmx";

	/** The txt_username. */
	private EditText txt_username;
	
	/** The txt_password. */
	private EditText txt_password;
	
	/** The btn_login. */
	private Button btn_login;
	
	/** The call service. */
	private CallService callService;
	
	/** The progress dialog. */
	private Dialog progressDialog;
	
	private Application apps;

	/**
	 * Called when the activity is first created.
	 *
	 * @param savedInstanceState the saved instance state
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// this is code config full screeen
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		Ultilities.mLockScreenRotation(this);
		setContentView(R.layout.screen_login);
		progressDialog = Ultilities.showProgressDialog(this);
		apps=(Application)getApplication();
		
		txt_username = (EditText) findViewById(R.id.txt_username);
		txt_password = (EditText) findViewById(R.id.txt_password);
		btn_login = (Button) findViewById(R.id.btn_login);
		btn_login.setOnClickListener(this);
	}

	/* (non-Javadoc)
	 * @see android.view.View.OnClickListener#onClick(android.view.View)
	 */
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_login:

			final Handler mHandler = new Handler();
			progressDialog.show();

			new Thread() {
				public void run() {
					try {
						Runnable r = new Runnable() {
							public void run() {
								// do your background process
								callService = new CallService();
								User user = callService.checkLogin(
										SOAP_ACTION, METHOD_NAME, NAMESPACE,
										URL, txt_username.getText().toString(),
										txt_password.getText().toString());
								
								 if (user.getMessage().equals("true")) {
									 Intent it = new Intent(StrykersActivity.this,
												HomeActivity.class);
										startActivity(it);
										apps.setUser(user);
								 }else{
									 validateForm(user.getMessage());
									 progressDialog.cancel();
									 return;
								 }
								overridePendingTransition(R.anim.slide_left, 0);
								progressDialog.cancel();
							}

						};
						mHandler.post(r);
					} catch (Exception e) {
					}
				}
			}.start();

			break;

		default:
			break;
		}

	}
	/**
	 * Validate form.
	 *
	 * @param message the message
	 */
	public void validateForm(String message) {
		Toast msg = Toast.makeText(this, message,
				Toast.LENGTH_SHORT);
		msg.setGravity(Gravity.CENTER, msg.getXOffset() / 2,
				msg.getYOffset() / 2);

		msg.show();
	}
}